# Copyright (c) 2024 orchy <orchy12@proton.me>
# This file is licensed under the MIT/Expat license.
# You should have received a copy of the license with this software, if not see
# https://spdx.org/licenses/MIT.html
import std/os
import std/paths
import std/osproc
import std/terminal
import ../src/neattui

var menu: TuiSingleSelection = TuiSingleSelection(pos: TuiPos(x: 0.5, y: 0.5),
                               selected: 0, options: @[".."], border: true)
while true:
  menu.options = @[".."]
  var fileList: seq[Path]
  for kind, path in walkDir("."):
    fileList.add Path(path)

  for file in fileList:
    menu.options.add file.extractFilename.string
  eraseScreen
  menu.validate
  menu.render
  case getch():
    of 'q':
      TuiEnd()
      quit()
    of 'j':
      menu.selected += 1
    of 'k':
      menu.selected -= 1
    of ' ':
      if getFileInfo(menu.selectedString()).kind == pcFile:
        discard waitForExit startProcess(getenv("PAGER"), "", [menu.selectedString()],
                            nil, {poParentStreams, poInteractive, poUsePath})
      else:
        setCurrentDir(menu.selectedString())
      menu.selected = 0
    else: discard
